﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;
using Lib.Helper;
using MSIACCSL2.AppParamServices;
using MSIACCSL2.ViewModels;
using WcfService1.DTO;

namespace MSIACCSL2.Views.PreSalesOrder
{
    public partial class Index : Page
    {

        public PreSalesOrderViewModel VM
        {
            get { return (PreSalesOrderViewModel)this.DataContext; }
        }

        public PreSalesOrderWrapViewModel WMW
        {
            get { return (this.Resources["POW"] as PreSalesOrderWrapViewModel); }
        }

        private string ReportUrl;
        private AppParamServices.AppParamServicesClient paramClient;
        public Index()
        {
            InitializeComponent();
            PreSalesOrderViewModel.OnSearchCompleted = (x) =>
                                          {
                                              this.DataContext = (PreSalesOrderViewModel)x;
                                          };
           
            paramClient = new AppParamServicesClient();
            //GetServerUrl();
        }

       

        private void btnSend_Click(object sender, System.Windows.RoutedEventArgs e)
        {
        	VM.SendEmail((sender as Button).DataContext);
        }

        private void btnPrint_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            var ps = (sender as Button).DataContext as PreSalesOrderDTO;
            //GlobalVar.OkaSocket.SendMessage("PreSalesOrder|" + ps.PSNo);
            MyButton button = new MyButton(ReportUrl + "PreSalesOrder.aspx?no=" + ps.PSNo);
            button.ClickMe();
        }

        public void GetServerUrl()
        {
            paramClient.GetAppParamListCompleted += new EventHandler<GetAppParamListCompletedEventArgs>(paramClient_GetAppParamListCompleted);
            paramClient.GetAppParamListAsync("ReportUrl");
        }

        void paramClient_GetAppParamListCompleted(object sender, GetAppParamListCompletedEventArgs e)
        {
            paramClient.GetAppParamListCompleted -= new EventHandler<GetAppParamListCompletedEventArgs>(paramClient_GetAppParamListCompleted);
            ReportUrl = e.Result.FirstOrDefault().AppParamValue;
        }
    }
}


